#include "MainWindow.h"
#include <SDL/SDL_image.h>
#include "CheckBox.h"

Image CheckBox::m_sprites[2];

CheckBox::CheckBox(IScreen* _caller, std::string const& _text)
: AButton(_caller, _text), m_checked(false)
{
	if (m_sprites[0].Surface == NULL)
	{
		m_sprites[0].Surface = IMG_Load("images\\CheckBox_unchecked.png");
		m_sprites[1].Surface = IMG_Load("images\\CheckBox_checked.png");
	}
	m_currentSprite = &m_sprites[0];
}

CheckBox::~CheckBox()
{
}

void CheckBox::updateState()
{
	if (m_currentState == AButton::ButtonState::PRESSED)
	{
		m_checked = !m_checked;
		if (m_checked == true)
		{
			m_currentSprite = &m_sprites[1];
		}
		else
		{
			m_currentSprite = &m_sprites[0];
		}
		SDL_Event& e = MainWindow::Event();
		e.type = SDL_MOUSEMOTION;
		SDL_PushEvent(&e);
	}
}

bool CheckBox::isChecked() const
{
	return (m_checked);
}

void CheckBox::setPosition(int _x, int _y)
{
	this->AButton::setPosition(_x, _y);
	if (m_text.Surface != NULL)
	{	
		if (m_currentSprite != NULL)
		{
			if (m_currentSprite->Surface != NULL)
			{
				m_text.Position.x = _x + m_currentSprite->Surface->w + 8;
				m_text.Position.y = _y + m_currentSprite->Surface->h / 2 - m_text.Surface->h / 2;
			}
		}
	}
}